﻿using Glorysoft.Auto.Xfw;
using Glorysoft.EAP.Core.Entities.DB;
using Glorysoft.EAP.Core.Entities.DB.Contract;
using Glorysoft.EAP.Core.Entities.DB.Service;
using Serilog.Core;
using Serilog.Events;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;


namespace Glorysoft.EAP.Core
{
    public class DbService //: //IDbService//: AbstractService
    {
        protected readonly XfwContext Context = XfwContext.Current;
        protected readonly Logger DBServerLog = XfwContext.LogManager.Create("DBServerLog");
        //public RPT_MSG_HISTORYService rptMsgHisService = null;
        //public RPT_RUN_HISTORY_TEMPService rptRunHisTempService = null;
        //public RPT_RUN_HISTORYService rptRunHisService = null;
        public virtual string Name => "DBServer";
        public string CurrentMethod => MethodBase.GetCurrentMethod().Name;

        public DbService()
        {
            try
            {
               // DBServerLog = XfwContext.LogManager.Create("DBServerLog"); //, LogEventLevel.Debug

                //rptMsgHisService =new RPT_MSG_HISTORYService();

                //rptRunHisTempService = new RPT_RUN_HISTORY_TEMPService();

                //rptRunHisService =new RPT_RUN_HISTORYService();

              
            }
            catch (Exception ex)
            {
                DBServerLog.Error($"{Name}.DbService() " + ex.Message);

            }
        }
        /// <summary>
        /// 添加操作记录信息
        /// </summary>
        /// <param name="msgInfo"></param>
        /// <param name="eqpID"></param>
        /// <param name="lotID"></param>
        /// <returns></returns>
        public int InsertMsgInfo(string msgInfo, string eqpID = "", string lotID = "")
        {
            try
            {
                RPT_MSG_HISTORY rptMsgHis = new RPT_MSG_HISTORY()
                {
                    EQP_ID = eqpID,
                    MESSAGE = msgInfo,
                    LOT_ID = lotID,
                    REAL_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")
                };
                return RPT_MSG_HISTORYService.Insert(rptMsgHis);
            }
            catch (Exception ex)
            {
                DBServerLog.Error($"{Name}.InsertMsgInfo() " + ex.Message);
                return -1;
            }
        }
        /// <summary>
        /// 添加Run货记录（已做完）
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int InsertRunHis(RPT_RUN_HISTORY entity)
        {
            try
            {
                return RPT_RUN_HISTORYService.Insert(entity);
            }
            catch (Exception ex)
            {
                DBServerLog.Error($"{Name}.InsertRunHis() " + ex.Message);
                return -1;
            }
        }
        /// <summary>
        ///  添加Run货记录（临时表，过程中）
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int InsertRunHisTemp(RPT_RUN_HISTORY_TEMP entity)
        {
            try
            {
                var rptRunHisTempList = RPT_RUN_HISTORY_TEMPService.SelectByEQPID(entity.EQP_ID, entity.LOT_ID, entity.PRODUCT_ID);
                foreach (var item in rptRunHisTempList)
                {
                    RPT_RUN_HISTORY_TEMPService.Delete(item.ID);
                }
                return RPT_RUN_HISTORY_TEMPService.Insert(entity);
            }
            catch (Exception ex)
            {
                DBServerLog.Error($"{Name}.InsertRunHisTemp() " + ex.Message);
                return -1;
            }
        }
        /// <summary>
        /// 删除Run货临时表
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int DeleteRunHisTemp(RPT_RUN_HISTORY_TEMP entity)
        {
            try
            {
                return RPT_RUN_HISTORY_TEMPService.Delete(entity.ID);
            }
            catch (Exception ex)
            {
                DBServerLog.Error($"{Name}.DeleteRunHisTemp() " + ex.Message);
                return -1;
            }
        }
        /// <summary>
        /// 通过设备ID，LotID获取批次信息
        /// </summary>
        /// <param name="eqpID">设备ID</param>
        /// <param name="lotID">批次ID</param>
        /// <returns></returns>
        public RPT_RUN_HISTORY_TEMP SeleteRunHsiTemp(string eqpID, string lotID)
        {
            try
            {
                //if (type == "0")
                //{
                //    return RPT_RUN_HISTORY_TEMPService.SelectByeqpID(eqpID, lotID);
                //}
                //else if (type == "1")
                //{
                //    return RPT_RUN_HISTORY_TEMPService.SelectByeqpIDAndLotID(eqpID, lotID);
                //}
                //else if (type == "2")
                //{
                //    return RPT_RUN_HISTORY_TEMPService.SelectByeqpIDAndLotIDProductID(eqpID, lotID);
                //}
                return RPT_RUN_HISTORY_TEMPService.SelectByeqpIDAndLotID(eqpID, lotID);
            }
            catch (Exception ex)
            {
                DBServerLog.Error($"{Name}.SeleteRunHsiTemp() " + ex.Message);
                return null;
            }
        }
        /// <summary>
        /// 修改Run货临时表数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int UpdateRunHsiTemp(RPT_RUN_HISTORY_TEMP entity)
        {
            try
            {
                return RPT_RUN_HISTORY_TEMPService.Update(entity);
            }
            catch (Exception ex)
            {
                DBServerLog.Error($"{Name}.UpdateRunHsiTemp() " + ex.Message);
                return -1;
            }
        }
        /// <summary>
        /// 增加Run货记录表，并删除Run货临时表
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int UpdateRunHsiTempByID(RPT_RUN_HISTORY_TEMP entity)
        {
            try
            {
         
                var RunHis = Mapper<RPT_RUN_HISTORY, RPT_RUN_HISTORY_TEMP>(entity);
                InsertRunHis(RunHis);

            
              
                return RPT_RUN_HISTORY_TEMPService.Delete(entity.ID);
            }
            catch (Exception ex)
            {
                DBServerLog.Error($"{Name}.UpdateRunHsiTempByID() " + ex.Message);
                return -1;
            }
        }
        /// <summary>
        /// 查询当前设备可以往MES出站的信息
        /// </summary>
        /// <param name="eqpID"></param>
        /// <returns></returns>
        public IList<RPT_RUN_HISTORY_TEMP> SelectRunHisTempByEQPID(string eqpID)
        {
            try
            {
                return RPT_RUN_HISTORY_TEMPService.QueryRunHisTempByEQPID(eqpID);
            }
            catch (Exception ex)
            {
                DBServerLog.Error($"{Name}.SelectRunHisTempByEQPID() " + ex.Message);
                return null;
            }
        }



        public BAS_EXC_CONFIG SelectExcMsgByCodeID(string codeID)
        {
            try
            {
                return BAS_EXC_CONFIGService.SelectExcMsgByCodeID(codeID);
            }
            catch (Exception ex)
            {
                DBServerLog.Error($"{Name}.SelectExcMsgByCodeID() " + ex.Message);
                return null;
            }
        }


        /// <summary>
        /// 两个同属性的类赋值
        /// </summary>
        /// <typeparam name="D">被赋值的类</typeparam>
        /// <typeparam name="S">赋值类</typeparam>
        /// <param name="s">赋值类</param>
        /// <returns>赋值后的结果</returns>
        public D Mapper<D, S>(S s)
        {
            D d = Activator.CreateInstance<D>();
            try
            {
                var sType = s.GetType();
                var dType = typeof(D);
                foreach (PropertyInfo sP in sType.GetProperties())
                {
                    foreach (PropertyInfo dP in dType.GetProperties())
                    {
                        if (dP.Name == sP.Name)
                        {
                            dP.SetValue(d, sP.GetValue(s));
                            break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                DBServerLog.Error($"Module: DBServer, Method: {nameof(Mapper)}", e);
            }
            return d;
        }
        /// <summary>
        /// 新增monitor监控数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="time"></param>
        /// <param name="weekid"></param>
        /// <param name="weektime"></param>
        /// <param name="monitottest"></param>
        /// <returns></returns>
        public int InsertMsgTo_Monitor_Time(int count, DateTime time, string weekid, DateTime weektime, string monitottest)
        {
            try
            {
                RPT_MONITOR_TIME_HISTORY rptMsgHis = new RPT_MONITOR_TIME_HISTORY()
                {
                    COUNT = count,
                    TIME = time,
                    WEEKID = weekid,
                    REMINDTIME = weektime,
                    MONITORTEST = monitottest

                };
                return RPT_MONITOR_TIME_HISTORYService.Insert(rptMsgHis);
            }
            catch (Exception ex)
            {
                DBServerLog.Error($"{Name}.InsertMsgInfo() " + ex.Message);
                return -1;
            }
        }
        /// <summary>
        /// 修改monitor监控数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="time"></param>
        /// <param name="weekid"></param>
        /// <param name="weektime"></param>
        /// <param name="monitortest"></param>
        /// <returns></returns>
        public int UpdateMsgTo_Monitor_Time(int count, DateTime time, string weekid, DateTime weektime, string monitortest)
        {
            try
            {
                RPT_MONITOR_TIME_HISTORY rptMsgHis = new RPT_MONITOR_TIME_HISTORY()
                {
                    COUNT = count,
                    TIME = time,
                    WEEKID = weekid,
                    REMINDTIME = weektime,
                    MONITORTEST = monitortest

                };
                return RPT_MONITOR_TIME_HISTORYService.Update(rptMsgHis.COUNT, rptMsgHis.WEEKID, rptMsgHis.MONITORTEST);
            }
            catch (Exception ex)
            {
                DBServerLog.Error($"{Name}.UpdateMsgTo_Monitor_Time() " + ex.Message);
                return -1;
            }
        }
        /// <summary>
        /// 查询monitor数据by周
        /// </summary>
        /// <param name="weekid"></param>
        /// <returns></returns>
        public RPT_MONITOR_TIME_HISTORY SeleteWeekid(string weekid)
        {
            try
            {
                return RPT_MONITOR_TIME_HISTORYService.SelectByWeekID(weekid);
            }
            catch (Exception ex)
            {
                DBServerLog.Error($"{Name}.SeleteRunHsiTemp() " + ex.Message);
                return null;
            }
        }
    }
}
